package com.conradharrison.example.simplecamera;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Build;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class */

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
	
    SurfaceHolder mHolder;
    Camera mCamera;
    
	@SuppressWarnings("deprecation")
	public CameraPreview(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback
        mHolder = getHolder();
        mHolder.addCallback(this);
        
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {	
			mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		}
    }

    public void surfaceCreated(SurfaceHolder holder) {

    	Log.v("Conrad", "surfaceCreated called");

    	// if camera is not ready...
    	if (mCamera == null) return;
    	
		setAndStartPreview(holder);

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    	Log.v("Conrad", "surfaceChanged called");

    	// if camera is not ready...
    	if (mCamera == null) return;
    	
    	// Set preview surface dependent camera parameters here
    	// TODO

    	// Preview maybe already set, but thats okay
		setAndStartPreview(holder);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {

    	Log.v("Conrad", "surfaceDestroyed called");
    	
    	// if camera is not ready...
    	if (mCamera == null) return;
        
    	mCamera.stopPreview();
    }

    private void setAndStartPreview(SurfaceHolder holder)
    {
    	// Preview maybe already set, but thats okay
        try {
        	Camera.Parameters parameters = mCamera.getParameters();
        	
        	Log.d("Conrad", "Current Picture format is " + parameters.getPictureFormat());
        	Log.d("Conrad", "Current Preview format is " + parameters.getPreviewFormat());
        	
        	List<Size> sizes = parameters.getSupportedPictureSizes();
        	for (int i=0;i<sizes.size();i++){
        	    Log.d("Conrad", "Supported Picture Size: " + sizes.get(i).width + "x" + sizes.get(i).height);         
        	}
        	
        	List<Integer> formats = parameters.getSupportedPictureFormats();
        	for (int i=0;i<formats.size();i++){
        	    Log.d("Conrad", "Supported Picture Format: " +formats.get(i));         
        	}
        	
        	sizes = parameters.getSupportedPreviewSizes();
        	for (int i=0;i<sizes.size();i++){
        	    Log.d("Conrad", "Supported Preview Size: " + sizes.get(i).width + "x" + sizes.get(i).height);         
        	}
        	
        	formats = parameters.getSupportedPreviewFormats();
        	for (int i=0;i<formats.size();i++){
        	    Log.d("Conrad", "Supported Preview Format: " +formats.get(i));         
        	}       	
        	//parameters.setPreviewSize(320, 240);
        	//parameters.setPreviewFormat(ImageFormat.NV21);
        	//parameters.setPictureSize(3264, 2176);
        	//parameters.setPictureFormat(ImageFormat.NV21);
        	mCamera.setParameters(parameters);
        	
        	mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.d("Conrad", "setAndStartPreview() Error setting camera preview: " + e.getMessage());
        }
        
    	// Preview maybe already running, but thats okay
        mCamera.startPreview();    	
    }


}
